﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Editor.PageControls;
using Editor.Utilities;

namespace Editor
{
    public partial class AddLabelForm : AddFormBase, IDictSubject<string>
    {
        private PageLabel pageLabel;

        public AddLabelForm()
        {
            InitializeComponent();
        }

        public override PageControl CreatedControl
        {
            get
            {
                return pageLabel;
            }
            set
            {
                pageLabel = (PageLabel)value;
            }
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("İsim girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!EditorForm.IsElementNameAvailable(textBoxName.Text.Trim()))
            {
                MessageBox.Show("Bu isim zaten var!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (textBoxText.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Yazı girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            float fontSize = -1f;

            if (!float.TryParse(textBoxFont.Text, System.Globalization.NumberStyles.Float, CultureInfo.CurrentCulture,
                out fontSize) || fontSize <= 0)
            {
                MessageBox.Show("Font büyüklüğü kesirli pozitif bir sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int maxWidth = -1;

            if (!int.TryParse(textBoxMaxWidth.Text, out maxWidth) || maxWidth <= 0)
            {
                MessageBox.Show("Azami genişlik bir pozitif tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            
            pageLabel.Name = textBoxName.Text.Trim();

            
            string textToAdd = textBoxText.Text;

            if (textToAdd != string.Empty && !textToAdd.EndsWith("\n"))
                textToAdd += "\n";

            string selectedLanguage = comboBoxLanguage.SelectedItem as string;

            Dictionary[selectedLanguage] = textToAdd;
            pageLabel.Dictionary = Dictionary;
            //pageLabel.AddText(PageLabel.SupportedLanguages[0], textToAdd);
            //pageLabel.Text = textBoxText.Text;
            pageLabel.BackColor = Color.White;
            pageLabel.ForeColor = Color.Black;
            pageLabel.TextFontSize = fontSize;
            pageLabel.Visible = true;
            pageLabel.MaxWidth = maxWidth;
            pageLabel.UpdateInnerControl();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void AddLabelForm_Load(object sender, EventArgs e)
        {
            pageLabel = new PageLabel();
            Dictionary = new SDictionary<string>();
            textBoxFont.Text = "9" + CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator + "5";
            textBoxName.Text = EditorForm.GenerateName(pageLabel.NamingPrefix);
            comboBoxLanguage.Items.AddRange(EditorForm.SupportedLanguages);
            comboBoxLanguage.SelectedIndex = 0;
        }

        protected void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            //save change
            string textToAdd = textBoxText.Text;

            if (textToAdd != string.Empty && !textToAdd.EndsWith("\n"))
                textToAdd += "\n";

            SDictionary<string> dict = Dictionary;
            textBoxText.Text = changeSubject(comboBoxLanguage.SelectedItem as string,
                comboBoxLanguage.SelectedIndex, textToAdd, ref dict);
            Dictionary = dict;
        }

        public SDictionary<string> Dictionary { get; set; }
    }
}
